package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishSetmealDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author lvFF
 * @Date 2022/04/29/下午 3:01;
 * Description
 */
@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;
    /**
     * 带条件分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        /**
         * Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        //查询条件
        setmealQueryWrapper.eq(StringUtils.isNotBlank(name), "name", name);
        //排序条件
        setmealQueryWrapper.orderByDesc("update_time");
        //
        setmealService.page(setmealPage,setmealQueryWrapper);
        //
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        //拷贝
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        //填充数据
        ArrayList<SetmealDto> list = new ArrayList<>();
        for (Setmeal record : setmealPage.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            //拷贝对象
            BeanUtils.copyProperties(record, setmealDto);
            //根据id查询对象
            Category byId = categoryService.getById(record.getCategoryId());
            //修改name
            setmealDto.setCategoryName(byId.getName());
            //添加
            list.add(setmealDto);
        }
        //添加
        setmealDtoPage.setRecords(list);
        //setmealService.page();
         */
        Page<SetmealDto> setmealDtoPage = setmealService.pageWith(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    /**
     * 套餐添加
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("套餐添加成果");
    }
    @DeleteMapping
    public R<String> delete(Long[] ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    @GetMapping("list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,
                "category_id",
                setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,
                "status",
                setmeal.getStatus());
        queryWrapper.orderByDesc("update_time");
        List<Setmeal> setmealList = setmealService.list(queryWrapper);

        return R.success(setmealList);
    }

    @GetMapping("dish/{Id}")
    public R<DishSetmealDto> listDish(@PathVariable("Id") Long Id){
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",Id);
        //queryWrapper.eq("status",)
       // queryWrapper.eq("status", 1);

        //queryWrapper.orderByDesc("sort").orderByDesc("update_time");
        List<Dish> list = dishService.list(queryWrapper);
        String DishName = null;
        for (Dish dish : list) {
            DishName = dish.getName();

        }
        log.info("list--------{}",list);

        QueryWrapper<Setmeal> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id",Id);
        queryWrapper1.eq("status", 1);
        //queryWrapper1.orderByDesc("sort").orderByDesc("update_time");
        List<Setmeal> list1 = setmealService.list(queryWrapper1);
        String SetmealName = null;
        for (Setmeal setmeal : list1) {
            SetmealName = setmeal.getName();
        }
        log.info("list1--------{}",list1);
        ArrayList<DishSetmealDto> dishSetmealDtos = new ArrayList<>();

        DishSetmealDto dishSetmealDto = new DishSetmealDto();
        dishSetmealDto.setSetmealS(list1);
        dishSetmealDto.setSetmealName(SetmealName);
        dishSetmealDto.setDishS(list);
        dishSetmealDto.setDishName(DishName);

        // log.info("imsge-----------{}",image);

        return R.success(dishSetmealDto);
    }
}
